﻿Imports System.Windows.Forms
Public Class FolderListEditor
  Public Sub New()
    InitializeComponent()
  End Sub

  Public Sub New(ByVal newFolders As List(Of String))
    Me.New()
    Me.Folders = newFolders
    Me.InputPanelMain_EnabledChanged(Me, EventArgs.Empty)
  End Sub

  Public Overrides Property BackColor() As System.Drawing.Color
    Get
      Return MyBase.BackColor
    End Get
    Set(ByVal value As System.Drawing.Color)
      MyBase.BackColor = value
      Me.PanelBottom.BackColor = value
      Me.PanelTop.BackColor = value
      Me.PanelPushUp.BackColor = value
      Me.WP7ButtonAdd.BackColor = value
      Me.WP7ButtonRemove.BackColor = value
      Me.ListBoxFolders.BackColor = value
    End Set
  End Property

  Public Overrides Property ForeColor() As System.Drawing.Color
    Get
      Return MyBase.ForeColor
    End Get
    Set(ByVal value As System.Drawing.Color)
      MyBase.ForeColor = value
      Me.WP7ButtonAdd.ForeColor = value
      Me.WP7ButtonRemove.ForeColor = value
      Me.ListBoxFolders.ForeColor = value
    End Set
  End Property

  Public Property Folders() As List(Of String)
    Get
      Dim fs As New List(Of String)
      For Each s As String In Me.ListBoxFolders.Items
        fs.Add(s)
      Next
      Return fs
    End Get
    Set(ByVal value As List(Of String))
      Me.ListBoxFolders.Items.Clear()
      For Each f As String In value
        Me.ListBoxFolders.Items.Add(f.Trim)
      Next
      Me.ListBoxSort()
    End Set
  End Property

  Public Sub FolderListEditor_Load() Handles Me.Load
    Me.WP7ButtonAdd.Text = Language.GetLanguageString("Add")
    Me.WP7ButtonRemove.Text = Language.GetLanguageString("Remove")
    Me.MenuItemOK.Text = Language.GetLanguageString("OK")
    Me.MenuItemCancel.Text = Language.GetLanguageString("Cancel")
  End Sub

  Private Sub WP7ButtonAdd_MouseUp(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles WP7ButtonAdd.MouseUp
    Dim sf As New SelectFile(SelectFile.Mode.FoldersOnly, Language.GetLanguageString("SelectFolder"))
    If sf.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
      For Each s As String In Me.ListBoxFolders.Items
        If String.Compare(s, sf.Folder, True) = 0 Then
          MessageBox.Show(Language.GetLanguageString("FolderAlreadyExists"), "Avian Play")
          Exit Sub
        End If
      Next
      Me.ListBoxFolders.Items.Add(sf.Folder)
      Me.ListBoxSort()
    End If
  End Sub

  Private Sub WP7ButtonRemove_MouseUp(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles WP7ButtonRemove.MouseUp
    If Me.ListBoxFolders.SelectedIndex >= 0 Then
      Dim si As Integer = Me.ListBoxFolders.SelectedIndex
      Me.ListBoxFolders.Items.RemoveAt(Me.ListBoxFolders.SelectedIndex)
      If si < Me.ListBoxFolders.Items.Count Then
        Me.ListBoxFolders.SelectedIndex = si
      ElseIf si > 0 AndAlso si - 1 < Me.ListBoxFolders.Items.Count Then
        Me.ListBoxFolders.SelectedIndex = si - 1
      ElseIf Me.ListBoxFolders.Items.Count > 0 Then
        Me.ListBoxFolders.SelectedIndex = 0
      End If
    End If
  End Sub

  Private Sub MenuItemOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemOK.Click
    Me.DialogResult = Windows.Forms.DialogResult.OK
    Me.Close()
  End Sub

  Private Sub MenuItemCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemCancel.Click
    Me.DialogResult = Windows.Forms.DialogResult.Cancel
    Me.Close()
  End Sub

  Private Sub ListBoxSort()
    Dim arr As New ArrayList
    Dim i As Integer
    arr.AddRange(ListBoxFolders.Items)
    arr.Sort()
    ListBoxFolders.Items.Clear()
    For i = 0 To arr.Count - 1
      ListBoxFolders.Items.Add(arr(i))
    Next i
    If Me.ListBoxFolders.Items.Count > 0 Then
      Me.ListBoxFolders.SelectedIndex = 0
    End If
  End Sub

  Private Sub InputPanelMain_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InputPanelMain.EnabledChanged
    If Me.InputPanelMain.Enabled Then
      Me.PanelPushUp.Height = Me.InputPanelMain.Bounds.Height
    Else
      Me.PanelPushUp.Height = 0
    End If
  End Sub
End Class